import sys
import string
import datetime
import time

def get_file_name(udate, utime):
	y = int('20' + udate[4:])
	dt = datetime.datetime(y, int(udate[2:4]), int(udate[0:2]),
							int(utime[0:2]), int(utime[2:4]), int(utime[4:6]))
	tt = dt.timetuple()
	return str(tt[0]) + str(tt[1]).zfill(2) + str(tt[2]).zfill(2) + '_' + str(tt[3]).zfill(2) + str(tt[4]).zfill(2) + '.txt'

def is_new_file(cur_dt, last_dt):
	y = int('20' + cur_dt[0][4:])
	dt = datetime.datetime(y, int(cur_dt[0][2:4]), int(cur_dt[0][0:2]),
							int(cur_dt[1][0:2]), int(cur_dt[1][2:4]), int(cur_dt[1][4:6]))
	utcs1 = time.mktime(dt.timetuple())

	y = int('20' + last_dt[0][4:])
	dt = datetime.datetime(y, int(last_dt[0][2:4]), int(last_dt[0][0:2]),
							int(last_dt[1][0:2]), int(last_dt[1][2:4]), int(last_dt[1][4:6]))
	utcs2 = time.mktime(dt.timetuple())

	if abs(utcs2 - utcs1) > 1800: # 30 Minutes * 60
		return True
	else:
		return False

def create_kml(src_filename):
	from xml.dom.minidom import parse
	import sys
	import string
	src_f = open(src_filename)
	path_name = src_filename.rstrip('.txt')
	coordinate = ''
	for line in src_f:
		lst_line = line.split(',')
		# print line, lst_line

		long_f = lst_line[5].split('.')[0]
		long_s = lst_line[5].split('.')[1]
		long_1 = int(long_f)/100
		long_2 = float('0.' + long_f[len(long_f)-2:] + long_s) * 1.6666
		longitude = str(long_1 + long_2)
		coordinate = coordinate + longitude + ','

		lat_f = lst_line[3].split('.')[0]
		lat_s = lst_line[3].split('.')[1]
		lat_1 = int(lat_f)/100
		lat_2 = float('0.' + lat_f[len(lat_f)-2:] + lat_s) * 1.6666
		latitude = str(lat_1 + lat_2)
		coordinate = coordinate + latitude + ' '

	dom = parse('path_tempelet.xml')
	eles = dom.getElementsByTagName("name")
	for ele in eles:
		name = dom.createTextNode(path_name)
		ele.appendChild(name)
	coor_eles = dom.getElementsByTagName("coordinates")
	name = dom.createTextNode(coordinate)
	coor_eles[0].appendChild(name)

	kml_f = file(path_name + '.kmz', 'w')
	print >> kml_f, dom.toxml()

	kml_f.close()
	dom.unlink()


if __name__ == '__main__':
	fn = sys.argv[1]
	sf = open(fn)
	last_line = None
	df = None

	try:
		for line in sf:
			cur_line = line.split(',')
			if last_line == None:
				fn = get_file_name(cur_line[9], cur_line[1])
				df = file(fn, 'w')
				last_line = cur_line
				cur_line[11] = cur_line[11][0:3]
				print >> df, string.join(cur_line, ',')
			else:
				cur_dt = [cur_line[9], cur_line[1]]
				last_dt = [last_line[9], last_line[1]]
				if is_new_file(cur_dt, last_dt):
					df.close()
					create_kml(fn)
					
					fn = get_file_name(cur_line[9], cur_line[1])
					df = file(fn, 'w')
					last_line = cur_line
					cur_line[11] = cur_line[11][0:3]
					print >> df, string.join(cur_line, ',')
				else:
					last_line = cur_line
					cur_line[11] = cur_line[11][0:3]
					print >> df, string.join(cur_line, ',')
	
	finally:
		df.close()
		sf.close()
		create_kml(fn)
